package com.asman.worker.widgets.gsyVideoPlayer

import android.content.Context
import android.os.Handler
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.widget.ProgressBar
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
import kotlinx.android.synthetic.main.empty_control_video.view.*
import com.asman.worker.R

/**
 * @author ybk
 * @date :2019-10-14 15:46
 * @desc:
 */
class EmptyControlVideo : StandardGSYVideoPlayer {

//    private var animationView: LottieAnimationView? = null

    private var progressBar: ProgressBar? = null

    var listener: OnPlayStatusListener? = null

    constructor(context: Context, fullFlag: Boolean?) : super(context, fullFlag!!) {}

    constructor(context: Context) : super(context) {}

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}

    var isAutoPause = false//是否是自动暂停的---是自动暂停的回到该页面自动播放

    override fun init(context: Context) {
        super.init(context)
//        animationView = findViewById(R.id.animationView)
        progressBar = findViewById(R.id.progressBar)

//        if (context is AppCompatActivity) {
//            context.lifecycle.addObserver(object : LifecycleObserver {
//                @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
//                fun onResume() {
//                    if (isAutoPause) {
//                        isAutoPause = false
//                        startPlay()
//                    }
//                }
//
//                @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
//                fun onPause() {
//                    pausePlay()
//                    isAutoPause = true
//                }
//
//                @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
//                fun onDestroy() {
//                    release()
//                }
//            })
//        }
        setVideoAllCallBack(object :MyVideoAllCallBack(){
            override fun onPlayError(url: String?, vararg objects: Any?) {
                if (listener != null) {
                    listener!!.error()
                }
            }
        })
    }


    override fun getLayoutId(): Int {
        return R.layout.empty_control_video
    }

    override fun touchSurfaceMoveFullLogic(absDeltaX: Float, absDeltaY: Float) {
        super.touchSurfaceMoveFullLogic(absDeltaX, absDeltaY)
        //不给触摸快进，如果需要，屏蔽下方代码即可
        mChangePosition = false

        //不给触摸音量，如果需要，屏蔽下方代码即可
        mChangeVolume = false

        //不给触摸亮度，如果需要，屏蔽下方代码即可
        mBrightness = false
    }

    override fun touchDoubleUp() {
        //super.touchDoubleUp();
        //不需要双击暂停
    }

    override fun addTextureView() {
        mTextureView = CustomRenderView()
        mTextureView.addView(
            context,
            mTextureViewContainer,
            mRotate,
            this,
            this,
            mEffectFilter,
            mMatrixGL,
            mRenderer,
            mMode
        )
    }

    ///////////////////////// ///////////////////////// /////////////////////////
    /////////////////////////自定义加载动画 /////////////////////////
    ///////////////////////// ///////////////////////// /////////////////////////
    override fun changeUiToNormal() {
        Log.d("-=-=-=", "changeUiToNormal")
        super.changeUiToNormal()
        reset()
    }

    override fun changeUiToPreparingShow() {
        Log.d("-=-=-=", "changeUiToPreparingShow")
        super.changeUiToPreparingShow()
        _20SecondCount()
        start()
    }

    override fun changeUiToPlayingShow() {
        Log.d("-=-=-=", "changeUiToPlayingShow")
        super.changeUiToPlayingShow()
        //第一次 isPrepared 显示提示信息
        if (!isPrepared) {
            toastShow("工地施工时间：\n非节假日 周一至周五\n上午 8:00~12:00\n下午 14:00~18:00", 5000)
            isPrepared = true
        }
        reset()
        if (listener != null) {
            listener!!.play()
        }
    }

    override fun changeUiToPauseShow() {
        Log.d("-=-=-=", "changeUiToPauseShow")
        super.changeUiToPauseShow()
        reset()
        if (listener != null) {
            listener!!.pause()
        }
    }

    override fun changeUiToPlayingBufferingShow() {
        Log.d("-=-=-=", "changeUiToPlayingBufferingShow")
        super.changeUiToPlayingBufferingShow()
        start()
    }

    override fun changeUiToCompleteShow() {
        Log.d("-=-=-=", "changeUiToCompleteShow")
        super.changeUiToCompleteShow()
        reset()
    }

    override fun changeUiToError() {
        Log.d("-=-=-=", "changeUiToError")
        super.changeUiToError()
        reset()
        if (listener != null) {
            listener!!.error()
        }
    }

    override fun changeUiToPrepareingClear() {
        Log.d("-=-=-=", "changeUiToPrepareingClear")
        super.changeUiToPrepareingClear()
        reset()
    }

    override fun changeUiToPlayingBufferingClear() {
        Log.d("-=-=-=", "changeUiToPlayingBufferingClear")
        super.changeUiToPlayingBufferingClear()
        start()
    }

    override fun changeUiToClear() {
        Log.d("-=-=-=", "changeUiToClear")
        super.changeUiToClear()
        reset()
    }

    override fun changeUiToCompleteClear() {
        Log.d("-=-=-=", "changeUiToCompleteClear")
        super.changeUiToCompleteClear()
        reset()
    }

    private fun start() {
//        animationView?.visibility = View.VISIBLE
//        animationView?.playAnimation()
        progressBar?.visibility = View.VISIBLE
    }

    private fun reset() {
//        animationView?.visibility = View.GONE
//        animationView?.pauseAnimation()
        progressBar?.visibility = View.GONE
    }

    fun changeStatus() {
        clickStartIcon()
    }

    //开始播放
    fun startPlay() {
        if (currentState != CURRENT_STATE_PLAYING) {
            changeStatus()
        }
    }

    //暂停播放
    fun pausePlay() {
        if (currentState != CURRENT_STATE_PAUSE) {
            changeStatus()
        }
    }

    fun setOnPlayStatusListener(listener: OnPlayStatusListener) {
        this.listener = listener
    }

    interface OnPlayStatusListener {
        fun play()

        fun pause()

        fun error()
    }

    fun toast(text: String) {
        toastShow(text, 2000) {
        }
    }

    fun toastShow(text: String, delayMillis: Long, hide: (() -> Unit)? = null) {
        tv_toast.text = text
        tv_toast.visibility = View.VISIBLE
        tv_toast.postDelayed({
            tv_toast.visibility = View.GONE
            hide?.invoke()
        }, delayMillis)
    }

    var connectListener: (() -> Unit)? = null

    fun setConnnectTimeOut(l: (() -> Unit)) {
        connectListener = l
    }

    var isPrepared = false

    //20秒加载时间
    val _20Runnable = {
        if (!isPrepared) {
            release()
//            animationView?.visibility = View.GONE
            progressBar?.visibility = View.GONE
            connectListener?.invoke()
            toastShow("未连上您家工地的摄像机，请稍后再试", 2000) {
            }
            if (listener != null) {
                listener!!.error()
            }
        }
    }

    fun _20SecondCount() {
        Handler().postDelayed(_20Runnable, 20000)
    }

    //Android 动态addView RN 页面不刷新问题
    override fun requestLayout() {
        super.requestLayout()
        post(measureAndLayout)
    }

    private val measureAndLayout = Runnable {
        measure(
            MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
            MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)
        )
        layout(left, top, right, bottom)
    }
}